package com.aiyige.utils.widget;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;

import com.aiyige.R;
import com.aiyige.utils.ListUtil;
import com.aiyige.utils.ToastX;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * Created by zhuyakun on 2017/11/6.
 */

public class CommonBottomDialogFragment extends BaseBottomDialogFragment implements View.OnClickListener {
    int position = 0;
    Listener listener = null;
    List<Integer> viewIdList = new ArrayList<>();
    int layoutResId = 0;

    public CommonBottomDialogFragment() {
    }

    public static Builder newBuilder() {
        return new Builder();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(layoutResId, container, false);
        if (!ListUtil.isEmpty(viewIdList)) {
            for (Integer id : viewIdList) {
                try {
                    view.findViewById(id).setOnClickListener(this);
                } catch (Exception e) {
                    //ToastX.show(e.getMessage());
                }
            }
        }
        return view;
    }

    public int getLayoutResId() {
        return layoutResId;
    }

    public void setLayoutResId(int layoutResId) {
        this.layoutResId = layoutResId;
    }

    public List<Integer> getViewIdList() {
        return viewIdList;
    }

    public void setViewIdList(List<Integer> viewIdList) {
        this.viewIdList = viewIdList;
    }

    public Listener getListener() {
        return listener;
    }

    public void setListener(Listener listener) {
        this.listener = listener;
    }

    public int getPosition() {
        return position;
    }

    public void setPosition(int position) {
        this.position = position;
    }

    @Override
    public void onClick(View v) {
        if (listener != null) {
            listener.onViewClicked(position, v);
        }
        dismiss();
    }

    public interface Listener {
        void onViewClicked(int position, View view);
    }


    public static final class Builder {
        private Listener listener;
        private List<Integer> viewIdList;
        private int layoutResId;

        private Builder() {
        }

        public Builder listener(Listener listener) {
            this.listener = listener;
            return this;
        }

        public Builder viewIdList(List<Integer> viewIdList) {
            this.viewIdList = viewIdList;
            return this;
        }

        public Builder viewIdList(int... viewIds) {
            this.viewIdList = new ArrayList<>();
            for (int id : viewIds) {
                viewIdList.add(id);
            }
            return this;
        }

        public Builder layoutResId(int layoutResId) {
            this.layoutResId = layoutResId;
            return this;
        }

        public CommonBottomDialogFragment build() {
            CommonBottomDialogFragment commonBottomDialogFragment = new CommonBottomDialogFragment();
            commonBottomDialogFragment.setListener(listener);
            commonBottomDialogFragment.setViewIdList(viewIdList);
            commonBottomDialogFragment.setLayoutResId(layoutResId);
            return commonBottomDialogFragment;
        }
    }
}
